using System;
using System.Collections.Generic;
using System.Linq;

namespace jSLCore.Client.Presentation.jBinding.BaseConverters.BaseMultiConverter
{
    public class jBooleansMultiConverter : IMultiConverter
    {
        public object Convert(object[] values)
        {
            IEnumerable<bool> bools;
            try
            {
                bools = values.Select(v =>(v is bool) && (bool)v);
            }
            catch (InvalidCastException)
            {

                return DefaultValue;
            }
            

            bool result=DefaultValue;
            switch (OperatorType)
            {
                    case BooleanOperatorType.And:
                    if (bools.Count() == 0) return DefaultValue;
                    result = true;
                    foreach (var b in bools)
                    {
                        result = result && b;
                    }
                        break;

                    case BooleanOperatorType.Or:
                        if (bools.Count() == 0) return DefaultValue;
                        result = false;
                        foreach (var b in bools)
                        {
                            result = result || b;
                        }
                        break;
            }
            return result;
        }

        public BooleanOperatorType OperatorType { get; set; }
        public bool DefaultValue { get; set; }
    }

    
}